<?php

declare(strict_types=1);
/**
 * MineAdmin is committed to providing solutions for quickly building web applications
 * Please view the LICENSE file that was distributed with this source code,
 * For the full copyright and license information.
 * Thank you very much for using MineAdmin.
 *
 * @Author X.Mo<root@imoi.cn>
 * @Link   https://gitee.com/xmo/MineAdmin
 */

namespace Plugin\Attractions\Commerce\Service\Commerce_commodity;

use Plugin\Attractions\Commerce\Repository\Commerce_commodity\CommerceCommodityRepository;
use App\Service\IService;

/**
 * 商品服务类
 */
final class CommerceCommodityService extends IService
{
    public function __construct(
        protected readonly CommerceCommodityRepository $repository
    ) {}

    public function apiPage(array $params, int $page = 1, int $pageSize = 10): array
    {
        $result = $this->repository->perQuery(
            $this->repository->getQuery()
                ->select(['id', 'merchant_id', 'commodity_name', 'commodity_category', 
                'commodity_brand', 'commodity_model', 'commodity_spec', 'commodity_unit', 
                'commodity_description', 'sale_price', 'discount', 'full_amount', 'full_reduction', 
                'stock_quantity', 'sales_quantity', 'commodity_images', 'status', 'sort_order', 
                'remark'])
                ->with(['type:id,name', 'merchant:id,spot_id,merchant_name,contact_phone']),
            $params
        )->paginate(
            perPage: $pageSize,
            pageName: $this->repository::PER_PAGE_PARAM_NAME,
            page: $page,
        );
        return $this->repository->handlePage($result);
    }
}
